/* SquareOnBoard.java
 * Auti i method parousiazi ena Square pano sto Chess Board.
 * Boris na kani add kai remove ena piece( pou ine Jlable / Component).
 */

package chess;

import java.awt.*;
import javax.swing.*;
 
public class SquareOnBoard extends JPanel{
    
    /**
     * Kali ton Constractor super();
     */
    public SquareOnBoard(){ super(); }
    
    public SquareOnBoard(String name){
        this();
        setName(name);
    }
    
    public void setSquareName(String name){ setName(name); }
    
    /**
     * 
     * @param p
     */
    public void addPiece(Piece p){
        add(p);
        paintAll(getGraphics());
    }
    
    /**
     * An iparhoun Pieces ta aferi apo to Board.
     */
    public void removePiece(){
        if(getComponentCount() > 0){
            remove(0);
            paintAll(getGraphics());
        }
    }
    
    /**
     * Accessor.
     * Theti to Hroma tou Square kai to kani paint sto Chess Board.
     * @param c To hroma tou Square.
     */
    public void setSquareColor(Color c){
        setBackground(c);
        paintAll(getGraphics());
    }
    
    /**
     * 
     * @return Epistrefi mia boolean me true/false analoga me to an to Chess Board ine Empty ( getComponentCount()=0 ) i ohi.
     */
    public boolean isEmpty(){
        if(getComponentCount() == 0)
            return true;
        else
            return false;
    }
    
    /**
     *
     * @return Ton arithmos ton Piece pou vriskonde sto Chess Board.
     */
    public int getNumOfComponents(){ return getComponentCount(); }
    
    /**
     * @return To Piece sto sigekrimeno Square.
     */
    public Piece getPieceOnSquare(){
        if(getComponentCount() > 0)
            return (Piece)getComponent(0);
        else 
            return null;
    }
}